package test01;

import java.util.*;

public class Test_1062_03 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int t = scanner.nextInt();
        scanner.nextLine();
        List<StringBuilder> list = new ArrayList<>();
        for (int i = 0; i < t; i++) {
            list.add(new StringBuilder(scanner.nextLine()));
        }


        int start = 0;

        for (StringBuilder stringBuilder : list) {
            char first = ' ';
            if (stringBuilder.charAt(1) == ' '){
                first = stringBuilder.charAt(0);
            }
            stringBuilder.reverse();
            Stack<String> stack = new Stack<>();
            for (int i = 0; i < stringBuilder.length(); i++) {
                if (i>1&&stringBuilder.charAt(i-1) == ' ') {
                    stack.push(" ");
                    start = i;
                    continue;
                }
                if (stringBuilder.charAt(i) == ' ') {
                    stack.push(stringBuilder.substring(start, i));
                }
                if (i + 1 == stringBuilder.length()) {
                    stack.push(stringBuilder.substring(start));
                    start = 0;
                }
            }
            if (first != ' ')
                stack.push(first+"");
            while (!stack.isEmpty()) {
                System.out.print(stack.pop());
            }
            System.out.println();
        }
    }
}
